<template>
	<view>
		<nav-back></nav-back>
		<view class="welcome">
			<text>您好，
				欢迎注册</text>
		</view>

		<view class="login">
			<form @submit="formSubmit" @reset="formReset">
				<view class="my-input username">
					<input class="uni-input" name="username" @input='handlePhone' placeholder="请输入手机号" />
				</view>
				<view class="my-input password">
					<input class="uni-input" :password="isPassword" name="password" placeholder="请输入密码" />
					<image @click="showPassword(true)" v-if="!isPassword" src="/static/img/show.png" mode=""></image>
					<image @click="showPassword(false)" v-else src="/static/img/hidden.png" mode=""></image>
				</view>
				<view class="my-input password1">
					<input class="uni-input" :password="isPassword1" name="password1" placeholder="确认密码" />
					<image @click="showPassword1(true)" v-if="!isPassword1" src="/static/img/show.png" mode=""></image>
					<image @click="showPassword1(false)" v-else src="/static/img/hidden.png" mode=""></image>
				</view>
				<view class="my-input code">
					<input class="uni-input" name="code" placeholder="验证码" />
					<text class="send" v-if="codeCd==0" @click="sendCode">发送验证码</text>
					<text class="send-cd" v-else>重新发送（{{codeCd}}s）</text>
				</view>
				<!-- <view class="treaty-tip">
		      <view class="treaty">
		        <view class="agree" @click="handleAgree">
		          <image v-if="{{isAgree==false}}" src="/static/img/agree1.png" mode=""/>
		          <image v-if="{{isAgree==true}}" src="/static/img/agree2.png" mode=""/>
		        </view>
		        已同意 <text>《用户协议》</text> 和 <text>《隐私协议》</text>
		      </view>
		      <view class="trouble">接收不到验证码</view>
		    </view> -->
				<button formType="submit" type="primary">立即注册</button>
			</form>
		</view>

		<view class="resiger" @click="handleLogin">
			<text>已有账号？ </text> 去登录 
			<image src="/static/img/arrow.png" mode="" />
		</view>

	</view>
</template>

<script>
	import request from '@/utils/request.js'
	import NavBack from '@/components/NavBack.vue'

	export default {
		components: {
			NavBack
		},
		data() {
			return {
				isAgree: false,
				phone: '',
				codeCd: 0,
				isPassword:true,
				isPassword1:true,
				phoneReg: /^1[3456789]\d{9}$/
			}
		},
		methods: {
			handlePhone(e) {
				// console.log(e.detail.value,'handlePhone');
				this.phone = e.detail.value
			},
			showPassword(isShow){
				this.isPassword = isShow
			},
			showPassword1(isShow){
				this.isPassword1 = isShow
			},
			sendCode() {
				console.log(this.phone, 'sendCode');
				// 示例代码

				if (!this.phone) {
					uni.showToast({
						title: '请输入您的手机号',
						icon: 'none',
						duration: 2000
					})
					return
				}

				if (this.phoneReg.test(this.phone)) {
					console.log("手机号格式正确");
					this.codeCd = 60
					let timer = setInterval(() => {
						console.log('setInterval');
						this.codeCd = --this.codeCd
						if (this.codeCd == 0) {
							clearInterval(timer)
						}
					}, 1000)
					request(`/sms/send/${this.phone}`).then(((res) => {
						console.log(res, 'res');
					}))
				} else {
					console.log("手机号格式不正确");
					uni.showToast({
						title: '手机号格式错误',
						icon: 'none',
						duration: 1000
					})
				}
			},
			handleAgree() {
				this.isAgree = !this.isAgree
			},
			formSubmit(e) {
				// if(!this.isAgree){
				//   uni.showToast({
				//     title: '请同意用户协议以及隐私协议',
				//     icon: 'none',
				//     duration: 2000
				//   })
				//   return
				// }
				const account = e.detail.value
				console.log('form发生了submit事件，携带数据为：', e.detail.value)
				if (!account.username || !account.password ||
					!account.password1 || !account.code) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (!this.phoneReg.test(this.phone)) {
					uni.showToast({
						title: '手机号格式错误',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (account.password != account.password1) {
					uni.showToast({
						title: '密码不一致',
						icon: 'none',
						duration: 2000
					})
					return
				}
				request(`/employee/register`, {
					method: 'POST',
					data: e.detail.value
				}).then(((res) => {
					if (res.data.code == 1) {
						uni.showToast({
							title: '注册成功',
							icon: 'none',
							duration: 2000
						})
						uni.navigateTo({
							url: '/pages/userInfo/loginWay/loginWay'
						})
					} else {
						console.log(res.data.msg, 'res');
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000
						})
					}
				}))
			},
			handleLogin() {
				uni.navigateTo({
					url: '/pages/userInfo/loginWay/loginWay'
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		flex-direction: column;
		padding: 20rpx 50rpx;
		box-sizing: border-box;
	}
	button[type=primary] {
		line-height: 90rpx !important;
	}
	.welcome {
		margin-top: 60rpx;

		text {
			font-size: 48rpx;
			font-weight: 700;
			line-height: 70rpx;
			color: #333333;

		}
	}

	.ways {
		display: flex;
		font-size: 32rpx;
		color: rgba(102, 102, 102, 1);
		margin-top: 80rpx;

		.way {
			margin-right: 65rpx;
		}

		.active {
			color: blue;
		}

		.active::after {
			clear: both;
			content: "";
			display: block;
			width: 85%;
			margin: 0 auto;
			margin-top: -12rpx;
			border-radius: 20rpx;
			background-color: rgba(156, 156, 255, 1);
			;
			height: 20rpx;
			overflow: hidden
		}
	}

	.login {
		margin-top: 60rpx;

		.code .send {
			color: rgba(107, 107, 255, 1);
			margin: 0 20rpx;
		}

		.code .send-cd {
			color: rgba(204, 204, 204, 1);
			margin: 0 20rpx;
		}

		// .code text{
		//   color: rgba(107, 107, 255, 1);
		//   margin: 0 20rpx;
		// }
		.treaty-tip {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.treaty {
				justify-content: start;
				// background-color: rgb(143, 58, 58);
				// width: 60% !important;
				flex: 1;
			}

			.trouble {
				width: 30% !important;
				text-align: end;
				color: rgba(166, 166, 166, 1);
				font-size: 24rpx;
			}
		}

	}

	.resiger {
		display: flex;
		font-size: 28rpx;
		// background-color: rgb(175, 52, 52);
		align-items: center;
		color: rgba(107, 107, 255, 1);
		justify-content: center;
		margin-top: 200rpx;

		text {
			font-size: 28rpx;
			color: rgba(166, 166, 166, 1);
		}

		image {
			width: 24rpx;
			height: 20rpx;
			margin: 0 10rpx;
		}
	}
	.password,.password1{
		image{
			width: 48rpx;
			height: 48rpx;
		}
	}
</style>